﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PhotoGalleryEnteties;
using PhotogalleryModel;
using photogalleryMVC.Models.CommentModels;

namespace photogalleryMVC.Controllers
{
    public class CommentController : PhotoGalleryController 
    {
        //
        //GET: Comment/GetComments/id
        public ActionResult ShowComments(int id)
        {
            IEnumerable<PhotoComment> comments = CommentRepository.GetRootComments( id);
            if(comments.Count()==0)
            {
                comments = new List<PhotoComment>();
                
            }
            ViewData["PhotoId"] = id;
            if(User.Identity.IsAuthenticated )
            {
                ViewData["CurrentUserName"] = User.Identity.Name;
                GalleryUser user = UserRepository.GetUserByName(User.Identity.Name);
                ViewData["SubscribeExists"] = SubscribeRepository.SubscribeExists( user.UserId ,id);
            }
            return View("RootCommentBlock", comments);

        }

        //
        // Get: Comment/ReplyComment/id?replyId
        public ActionResult ReplyComment(int id,int? replyId)
        {
            if(replyId==null )
            {
                return RedirectToAction("Index", "Home");
            }
            PhotoComment comment = CommentRepository.GetComment(id);
            ViewData["author"] = comment == null ? string.Empty : comment.Author.UserName;
            ViewData["replyId"] = replyId.Value;
            ViewData["id"] = id;
            return View();
        }
        
        
        //
        // Get: /Comment/Create/id?replyId

        [Authorize ]
        public ActionResult Create (int id,int?replyId)
        {        
            RouteValueDictionary model = new RouteValueDictionary();
            model.Add("id", id);
            if (replyId.HasValue)
            {
                model.Add("replyId", replyId.Value);
                if(Request.IsAjaxRequest() )
                {
                    return View(model);

                }
                PhotoComment comment = CommentRepository.GetComment(replyId .Value );
                ViewData["Author"] = comment == null ? string.Empty : comment.Author.UserName;
                return View("ReplyComment", model);
               
            }
            return View(model);         
        }


        //
        // POST: /Comment/Create

        [HttpPost]
        [Authorize ]
        public ActionResult Create(FormCollection collection,int id,int? replyId)
        {
            PhotoComment newComment = CommentRepository.CreateComment();
            if(replyId.HasValue )
            {
                newComment.ParentComment = CommentRepository.GetComment(replyId.Value);
            }
            PhotoInfo photo = PhotoRepository.GetPhotoInfo(id);
            GalleryUser user = UserRepository.GetUserByName(User.Identity.Name);
            newComment.Author =user;
            newComment.Photo = photo ;
            UpdateModel(newComment);
            CommentRepository.ValidateComment(newComment).RegisterErrors(ModelState);
            if (ModelState.IsValid)
            {
                ModelContext .SaveChanges();
                //create comments subscribe
                if (!SubscribeRepository.SubscribeExists(user, photo))
                    SubscribeRepository.AddSubscribeForComments(user, photo, DateTime.Now);
               
            }

           return  RedirectToAction("Detail", "Photo", new { id = id });
        }
        
    

        //
        // GET: /Comment/Delete/5
        //[AccessToResource("id",ResourceType.Comment  )]
        public ActionResult Delete(int id)
        {
            PhotoComment comment = CommentRepository.GetComment(id);
            int photoId = comment.Photo.PhotoId;
            CommentRepository.DeleteComment(comment);
            ModelContext .SaveChanges();
            return RedirectToAction("Detail", "Photo", new {id = photoId});
        }

        //
        //GET:/Comment/AddSubsscribe/5?returnUrl

        public ActionResult AddSubscribe(int id,string returnUrl)
        {
            if(User.Identity.IsAuthenticated )
            {
                GalleryUser user = UserRepository.GetUserByName(User.Identity.Name);
                SubscribeRepository.AddSubscribeForComments(user.UserId, id, DateTime.Now);
            }
            if (string.IsNullOrEmpty(returnUrl))
                return RedirectToAction("Index", "Home");
            return Redirect(returnUrl);
        }

       
    }
}
